package com.lun.blog.controller.admin;
import com.lun.blog.pojo.Message;
import com.lun.blog.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

@Controller
@RequestMapping("/admin/message")
public class MessagesController {

    @Autowired
    MessageService messageService;

    @GetMapping("/loadMessage")
    public String loadMessage(@PageableDefault(size = 5,sort = {"createTime"},direction = Sort.Direction.DESC) Pageable pageable,
                              Model model){
        model.addAttribute("page",messageService.pageMessage(pageable));
        return "admin/message";
    }

    @GetMapping("/deleteMessage")
    public String deleteMessage(Long id, RedirectAttributes attributes){
        Message m = messageService.findById(id);
        messageService.deleteMessage(m);
        attributes.addFlashAttribute("message","删除成功");
        return "redirect:/admin/message/loadMessage";
    }

    @GetMapping("/explain")
    public String explain(Long id,RedirectAttributes attributes){
        messageService.explain(id);
        attributes.addFlashAttribute("message","审批成功");
        return "redirect:/admin/message/loadMessage";
    }

}
